package com.lwl.service.impl.scl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwl.dao.scl.TSclPlanMapper;
import com.lwl.dao.scl.TSclRecordMapper;
import com.lwl.entity.MyPage;
import com.lwl.entity.Tuser;
import com.lwl.entity.scl.TSclPlan;
import com.lwl.entity.scl.TSclRecord;
import com.lwl.service.scl.TSclPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (TSclPlan)表服务实现类
 *
 * @author makejava
 * @since 2022-10-29 18:46:15
 */
@Service
public class TSclPlanServiceImpl implements TSclPlanService {
   @Autowired
   private TSclPlanMapper tSclPlanMapper;
   @Autowired
   private TSclRecordMapper tSclRecordMapper;


   @Override
   public PageInfo queryAll(MyPage myPage, TSclPlan tSclPlan) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      List<TSclPlan> tSclPlans = tSclPlanMapper.queryAll(tSclPlan);
      PageInfo pageInfo = new PageInfo(tSclPlans);
      return pageInfo;
   }

   @Override
   public Boolean checkCode(TSclPlan tSclPlan) {
      int i = tSclPlanMapper.selectCount(tSclPlan);
      System.out.println("验证码已经存在===========================i = " + i);
      return i==0?true:false;
   }

   @Override
   public Boolean checkPlanname(String planname) {
      TSclPlan tSclPlan = new TSclPlan();
      tSclPlan.setPlanname(planname);
      int i = tSclPlanMapper.selectCount(tSclPlan);
      System.out.println("该计划名字已经存在===========================i = " + i);
      return i==0?true:false;
   }

   @Override
   public Boolean addOrUpdataPlan(TSclPlan tSclPlan, Tuser user) {
      int i = -1;
      if (tSclPlan.getId()==null){
         tSclPlan.setUid(user.getId());
         tSclPlan.setCreatetime(new Date());
         i = tSclPlanMapper.insert(tSclPlan);
      }else {
         tSclPlan.setUpdatetime(new Date());
         i = tSclPlanMapper.updateByPrimaryKeySelective(tSclPlan);
      }
      return i>0?true:false;
   }

   @Override
   public Boolean delPlan(Integer id,String code) {
      int i=-1;
      TSclRecord tSclRecord = new TSclRecord();
      tSclRecord.setYqcode(code);
      int i1 = tSclRecordMapper.selectCount(tSclRecord);
      if (i1 == 0){
         i = tSclPlanMapper.deleteByPrimaryKey(id);
      }
      return i>0?true:false;
   }

   @Override
   public Boolean updUserStatus(TSclPlan tSclPlan) {
      //根据id修改状态
      int i = tSclPlanMapper.updateByPrimaryKeySelective(tSclPlan);
      return i>0?true:false;
   }

   @Override
   public TSclPlan getByCode(TSclPlan tSclPlan) {
      tSclPlan.setStatus(1);
      TSclPlan tSclPlan1 = tSclPlanMapper.selectOne(tSclPlan);
      return tSclPlan1;
   }

   @Override
   public int getPlanId(String yqcode) {
      TSclPlan tSclPlan = new TSclPlan();
      tSclPlan.setCode(yqcode);
      TSclPlan tSclPlan1 = tSclPlanMapper.selectOne(tSclPlan);
      Integer planId = tSclPlan1.getId();
      System.out.println("获取到的计划的planId = " + planId);
      return planId;
   }
}
